﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class PlayerData
{
    #region 单例
    private PlayerData()
    {

    }
    private static PlayerData _instance;
    public static PlayerData Instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = new PlayerData();
            }
            return _instance;
        }
    }
    #endregion

    #region SunNum
    private int _sunNum;
    public int SunNum
    {
        get
        {
            return _sunNum;
        }
        set
        {
            if (_sunNum != value)
            {
                _sunNum = value;
                if (SunNumRefresh != null) SunNumRefresh();
            }
        }
    }
    public UnityAction SunNumRefresh;
    #endregion

    #region ShitNum
    private int _shitNum;
    public int ShitNum
    {
        get
        {
            return _shitNum;
        }
        set
        {
            if (value != _shitNum)
            {
                _shitNum = value;
                if (ShitNumRefresh != null) ShitNumRefresh();
            }
        }
    }
    public UnityAction ShitNumRefresh;
    #endregion

    #region Round
    private int _round;
    public int Round
    {
        get
        {
            return _round;
        }
        set
        {
            if (value != _round)
            {
                _round = value;
                if(_round >= Const.MaxRound)
                {
                    if(GlobalMessage.Instance.GameOverEvent != null) GlobalMessage.Instance.GameOverEvent.Invoke();
                }
                else
                {
                    if(RoundRefresh!=null) RoundRefresh.Invoke();
                }
            }
        }
    }
    public UnityAction RoundRefresh;
    #endregion
    public PlayerStatus Status;
    /// <summary>
    /// 初始化游戏
    /// </summary>
    public void InitGame()
    {
        ShitNum = Const.OrginShitNum;
        SunNum = Const.OrginSunNum;
        Round = 0;
        MapData.Instance.Init();
        Status = PlayerStatus.SetSeed;
    }
    /// <summary>
    /// 设置种子
    /// </summary>
    /// <param name="gridOfSeed"></param>
    public void SetSeed(GridData gridOfSeed)
    {
        TreeData.Instance.SetSeed(gridOfSeed);
        Status = PlayerStatus.GrowUp;
    }

    public List<GridData> GetUseableGridForGrowUp(TreePartData treePart)
    {
        if (treePart.GridPos.IsTarget)
        {
            return new List<GridData>();
        }
        var grids = MapData.Instance.GetUseableGrid(treePart.GridPos);
        switch (treePart.Type)
        {
            case TreePartType.Seed:
                return grids.FindAll(data=>data.TreePart == null);
            case TreePartType.Root:
                return grids.FindAll(data => data.PosY >= treePart.GridPos.PosY && data.TreePart == null);
            case TreePartType.Node:
                return grids.FindAll(data => data.PosY <= treePart.GridPos.PosY && data.TreePart == null);
            default:
                throw new ArgumentOutOfRangeException();
        }
    }


    public GrowUpRes GetResCost(GridData grid, TreePartData treePart)
    {
        var result = new GrowUpRes();
        result += TreePartData.GetGrowUpResCost(grid, treePart);
        return result;
    }

    public void SetGrowUpPos(GridData grid, TreePartData treePart)
    {
        CollectRes();
        TreeData.Instance.GrowUp(grid, treePart);
        var cost = GetResCost(grid, treePart);
        ShitNum -= cost.ShitNum;
        SunNum -= cost.SunNum;
        
        if (MapData.Instance.TargetsSuccessNum >= Const.TargetNum)
        {
            if (GlobalMessage.Instance.GameOverEvent != null)
            {
                GlobalMessage.Instance.GameOverEvent.Invoke();
            }
        }
        Round++;
    }

    public void WaitCollectRes()
    {
        CollectRes();
        Round++;
    }

	public void Reset()
	{
		MapData.Instance.Reset();
        TreeData.Instance.Reset();
	}

    public void Restart()
    {
        Reset();
        InitGame();
    }

    private void CollectRes()
    {
        GrowUpRes gr = TreeData.Instance.CollectRes();
        ShitNum += gr.ShitNum;
        SunNum += gr.SunNum;
		if (GlobalMessage.Instance.CollectResEvent!= null)GlobalMessage.Instance.CollectResEvent.Invoke(gr);
    }
}
